
package uestc.bitman.edxapp.chapter;

import uestc.bitman.edxapp.R;
import uestc.bitman.edxapp.chapter.CourseChapter.Section;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CourseChaptersAdapter extends BaseExpandableListAdapter {

    // private Context mContext;

    private LayoutInflater inflater;

    private List<CourseChapter> mChapterList;

    public CourseChaptersAdapter(Context context) {
        // mContext = context;
        mChapterList = new ArrayList<CourseChapter>();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.chapter_item, parent, false);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.chapter_text_view);
        textView.setText(mChapterList.get(groupPosition).chapterDisplayName);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.section_item, parent, false);
        }
        TextView textView = (TextView) convertView.findViewById(R.id.section_text_view);
        textView.setText(mChapterList.get(groupPosition).getSection(childPosition).sectionDisplayName);

        return convertView;
    }

    public void reloadCourseChapters(List<CourseChapter> chapters) {
        mChapterList = chapters;
    }
    
    public List<CourseChapter> getCourseChapters(){
        return mChapterList;
    }

    @Override
    public CourseChapter getGroup(int groupPosition) {
        return mChapterList.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return mChapterList.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Section getChild(int groupPosition, int childPosition) {
        return mChapterList.get(groupPosition).getSections().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChapterList.get(groupPosition).getSections().size();
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
